package com.example.warehouse.bus.controller;

import com.example.warehouse.bus.pojo.entity.BusCustomer;
import com.example.warehouse.bus.pojo.entity.BusProvider;
import com.example.warehouse.bus.pojo.param.BusCustomerAddNewParam;
import com.example.warehouse.bus.pojo.param.BusProviderAddNewParam;
import com.example.warehouse.bus.pojo.vo.BusCustomerStandardVO;
import com.example.warehouse.bus.pojo.vo.BusProviderStandardVO;
import com.example.warehouse.bus.service.IBusProviderService;
import com.example.warehouse.common.pojo.vo.PageData;
import com.example.warehouse.common.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author Linger
 * @date 2023-07-07 9:03
 * @desciption:基础板块供应商管理控制层
 */
@Slf4j
@RequestMapping("/busProvider")
@RestController
@Api(tags = "2.2 基础管理-供应商管理")
public class BusProviderController {

    @Autowired
    private IBusProviderService busProviderService;

    public BusProviderController() {
        log.info("创建控制器对象:BusProviderController");
    }

    @ApiOperation("添加供应商")
    @ApiOperationSupport(order = 101)
    @PreAuthorize("hasAuthority('/busProvider/add-new')")
    @PostMapping("/add-new")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "providerName", value = "供应商名称",required = true, dataType = "String"),
            @ApiImplicitParam(name = "connectionPerson", value = "联系人姓名",required = true, dataType = "String"),
            @ApiImplicitParam(name = "phone", value = "联系电话",required = true, dataType = "String"),
            @ApiImplicitParam(name = "zip", value = "邮政编码", dataType = "String"),
            @ApiImplicitParam(name = "telephone", value = "移动电话", dataType = "String"),
            @ApiImplicitParam(name = "address", value = "地址", dataType = "String"),
            @ApiImplicitParam(name = "bank", value = "开户行", dataType = "String"),
            @ApiImplicitParam(name = "account", value = "银行卡号", dataType = "String"),
            @ApiImplicitParam(name = "email", value = "邮箱", dataType = "String"),
            @ApiImplicitParam(name = "fax", value = "传真号", dataType = "String"),
            @ApiImplicitParam(name = "enable", value = "是否启用", dataType = "Integer")
    })
    public JsonResult addNewBusProvider(@Valid BusProviderAddNewParam busProviderAddNewParam) {
        log.debug("开始处理【新增供应商】的请求，参数：{}",busProviderAddNewParam);
        busProviderService.addNewBusProvider(busProviderAddNewParam);
        return JsonResult.ok();
    }

    @PostMapping ("/{id}/delete")
    @ApiOperation("根据ID删除供应商")
    @ApiOperationSupport(order = 201)
    @PreAuthorize("hasAuthority('/busProvider/delete')")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "供应商ID", required = true, dataType = "long")
    })
    public JsonResult deleteBusProviderById(@PathVariable Long id) {
        log.debug("开始处理【根据ID删除供应商】的请求，参数：{}", id);
        busProviderService.delete(id);
        return JsonResult.ok();
    }

    @GetMapping("/{id}/getById")
    @ApiOperation("根据ID查询供应商")
    @ApiOperationSupport(order = 251)
   @PreAuthorize("hasAuthority('/busProvider/getById')")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "供应商ID", required = true, dataType = "long")
    })
    public JsonResult getBusProviderById(@PathVariable Long id) {
        log.debug("开始处理【根据ID查询供应商】的请求，参数：{}", id);
        busProviderService.getStandardByProviderId(id);
        return JsonResult.ok(busProviderService.getStandardByProviderId(id));
    }
    @PostMapping ("/update")
    @ApiOperation("修改供应商信息")
    @ApiOperationSupport(order = 301)
    @PreAuthorize("hasAuthority('/busProvider/update')")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "providerName", value = "供应商名称",required = true, dataType = "String"),
            @ApiImplicitParam(name = "connectionPerson", value = "联系人姓名",required = true, dataType = "String"),
            @ApiImplicitParam(name = "phone", value = "联系电话", required = true, dataType = "String"),
            @ApiImplicitParam(name = "zip", value = "邮政编码", dataType = "String"),
            @ApiImplicitParam(name = "telephone", value = "移动电话", dataType = "String"),
            @ApiImplicitParam(name = "address", value = "地址", dataType = "String"),
            @ApiImplicitParam(name = "bank", value = "开户行", dataType = "String"),
            @ApiImplicitParam(name = "account", value = "银行卡号", dataType = "String"),
            @ApiImplicitParam(name = "email", value = "邮箱", dataType = "String"),
            @ApiImplicitParam(name = "fax", value = "传真号", dataType = "String"),
            @ApiImplicitParam(name = "enable", value = "是否启用", dataType = "String")
    })
    public JsonResult updateBusProviderById(BusProvider busProvider) {
        log.debug("开始处理【修改供应商信息】的请求，参数：{}", busProvider);
        busProviderService.update(busProvider);
        return JsonResult.ok();
    }

    @PostMapping("/query-get")
    @ApiOperation("模糊查询供应商信息")
    @ApiOperationSupport(order = 424)
    @PreAuthorize("hasAuthority('/busProvider/query-get')")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "providerName", value = "供应商名称", dataType = "String"),
            @ApiImplicitParam(name = "connectionPerson", value = "联系人姓名",dataType = "String"),
            @ApiImplicitParam(name = "phone", value = "联系电话", dataType = "String"),
            @ApiImplicitParam(name = "page", value = "页码", defaultValue = "1", dataType = "long")
    })
    public JsonResult queryByKeyWord(Integer page, @Param("providerName") String providerName, @Param("connectionPerson")String connectionPerson, @Param("phone")String phone){
        log.debug("开始处理【模糊查询供应商信息】的请求，参数：{},{},{}", providerName, connectionPerson,  phone);
        PageData<BusProviderStandardVO> result  =  busProviderService.queryByKeyWord(page,providerName, connectionPerson,  phone);
        System.out.println(result);
        return JsonResult.ok(result);
    }


    @GetMapping("/get-id")
    @ApiOperation("查询供应商列表")
    @ApiOperationSupport(order = 425)
    @PreAuthorize("hasAuthority('/busProvider/get-id')")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", defaultValue = "1", dataType = "long")
    })
    public JsonResult list(Integer page) {
        log.debug("开始处理【查询供应商列表】的请求，页码：{}", page);
        Integer pageNum = page == null ? 1 : page;
        PageData<BusProvider> pageData = busProviderService.list(pageNum);
        return JsonResult.ok(pageData);
    }

    @GetMapping("/get-idAsc")
    @ApiOperation("升序查询供应商列表")
    @ApiOperationSupport(order = 426)
    @PreAuthorize("hasAuthority('/busProvider/get-idAsc')")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", defaultValue = "1", dataType = "long")
    })
    public JsonResult listAsc(Integer page) {
        log.debug("开始处理【升序查询供应商列表】的请求，页码：{}", page);
        Integer pageNum = page == null ? 1 : page;
        PageData<BusProvider> pageData = busProviderService.listAsc(pageNum);
        return JsonResult.ok(pageData);
    }

    @GetMapping("/get-idDesc")
    @ApiOperation("降序查询供应商列表")
    @ApiOperationSupport(order = 427)
    @PreAuthorize("hasAuthority('/busProvider/get-idDesc')")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", defaultValue = "1", dataType = "long")
    })
    public JsonResult listDesc(Integer page) {
        log.debug("开始处理【降序查询供应商列表】的请求，页码：{}", page);
        Integer pageNum = page == null ? 1 : page;
        PageData<BusProvider> pageData = busProviderService.listDesc(pageNum);
        return JsonResult.ok(pageData);
    }

    @PostMapping("/ban/{id:[0-9]+}/enable")
    @PreAuthorize("hasAuthority('/busProvider/ban/update')")
    @ApiOperation("启用供应商")
    @ApiOperationSupport(order = 312)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "供应商ID", required = true, dataType = "long")
    })
    public JsonResult setEnable(@PathVariable Long id) {
        log.debug("开始处理【启用供应商】的请求，参数：{}", id);
        busProviderService.setProviderEnable(id);
        return JsonResult.ok();
    }

    @PostMapping("/ban/{id:[0-9]+}/disable")
    @PreAuthorize("hasAuthority('/busProvider/noban/update')")
    @ApiOperation("禁用供应商")
    @ApiOperationSupport(order = 313)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "供应商ID", required = true, dataType = "long")
    })
    public JsonResult setDisable(@PathVariable Long id) {
        log.debug("开始处理【禁用供应商】的请求，参数：{}", id);
        busProviderService.setProviderDisable(id);
        return JsonResult.ok();
    }



}
